import struct
from collections import OrderedDict

from ..base import BaseParser
from ...helper import get_volume


class GetSecurityList(BaseParser):

    def setParams(self, market, start):
        pkg = bytearray.fromhex(u'0c 01 18 64 01 01 06 00 06 00 50 04')
        pkg.extend(struct.pack('<HH', market, start))

        self.send_pkg = pkg

    def parseResponse(self, body_buf):
        (num,) = struct.unpack('<H', body_buf[:2])

        pos = 2

        stocks = []

        for i in range(num):
            # b'880023d\x00\xd6\xd0\xd0\xa1\xc6\xbd\xbe\xf9.9\x04\x00\x02\x9a\x99\x8cA\x00\x00\x00\x00'
            # 880023 100 中小平均 276782 2 17.575001 0 80846648

            one_bytes = body_buf[pos: pos + 29]
            pos += 29

            (code, volunit, name_bytes, reversed_bytes1, decimal_point, pre_close_raw, reversed_bytes2) = struct.unpack('<6sH8s4sBI4s', one_bytes)

            code = code.decode('utf-8')
            name = name_bytes.decode('gbk').rstrip('\x00')

            pre_close = get_volume(pre_close_raw)

            one = OrderedDict(
                [
                    ('code', code),
                    ('volunit', volunit),
                    ('decimal_point', decimal_point),
                    ('name', name),
                    ('pre_close', pre_close),
                ]
            )

            stocks.append(one)

        return stocks
